package com.finiac.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.finiac.model.Bill;

public class BillDAOImpl implements BillDAO {

private HibernateTemplate hibernateTemplate;
	
	public void setSessionFactory(SessionFactory sessionFactory){
		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
	}
	
	
	@SuppressWarnings("unchecked")
	@Override
	public long createBill(Bill bill)
	{
		hibernateTemplate.save(bill);
		Bill lastBill = new Bill();
		List<Bill> billList = hibernateTemplate.find("from Bill where id = (select max(id) from Bill)");
		for(int i=0;i<billList.size();i++)
		{
			lastBill = billList.get(i);
		}
		return lastBill.getId();
		
	}


	@SuppressWarnings("unchecked")
	@Override
	public long lastBillId() 
	{
		Bill lastBill = new Bill();
		List<Bill> billList = hibernateTemplate.find("from Bill where id = (select max(id) from Bill)");
		for(int i=0;i<billList.size();i++)
		{
			lastBill = billList.get(i);
		}
		return lastBill.getId();
		
	}


	@SuppressWarnings("unchecked")
	@Override
	public Bill selectBill(long id) {
		Bill bill = new Bill();
		List<Bill> billList = hibernateTemplate.find("from Bill where id ="+id);
		for(int i=0;i<billList.size();i++)
		{
			bill = billList.get(i);
		}
		return bill;
	}


	@Override
	public void updateBill(Bill bill) {
		hibernateTemplate.update(bill);
		
	}
	
	

}
